<?php

namespace App\Services\Payment\PayAdapter;


use App\Facades\PaymentConfigFacades;
use App\Services\Payment\AbstractPayment;

class WechatPayAdapter extends AbstractPayAdapter implements WechatPayAdapterInterface
{
    protected $paymentService;

    public function __construct(){
        $this->paymentService = PaymentConfigFacades::getPaymentObject('wechatPay');
    }

    /**
     * 微信支付统一入口
     *
     * @param array $order
     * $order['body']               支付描述
     * $order['total_fee']          总金额
     * $order['trade_type']         交易类型 JSAPI(公众号支付) NATIVE(扫码支付)
     * $order['product_id']         商品id 充值，入股传用户id；购买传产品id
     * $order['openId']             用户openid 交易类型为JSAPI不能为空
     * $order['attach']             校验数据  格式mid:type    微信原生支付不能为空
     * $order['out_trade_no']       商户订单号(从账单接口获取)
     * $order['is_sub_pay']         是否子商户支付 1 是 空为否
     *
     * @return array
     */
    public function pay($order)
    {

        if(empty($order['body'])){
            return ["code"=>1,"msg"=>'支付描述不能为空'];
        }
        if(empty($order['total_fee'])){
            return ["code"=>1,"msg"=>'总金额不能为空'];
        }
        if(empty($order['product_id'])){
            return ["code"=>1,"msg"=>'商品id不能为空'];
        }
        if(empty($order['trade_type'])){
            return ["code"=>1,"msg"=>'交易类型不能为空'];
        }
        if(empty($order['attach'])){
            return ["code"=>1,"msg"=>'附加数据不能为空'];
        }

        if($order['pay_type'] == AbstractPayment::PAY_CASH && $order['trade_type']=='JSAPI' && empty($order['openId'])){
            return ["code"=>1,"msg"=>'openId不能为空'];
        }

        if(empty($order['out_trade_no'])){
            return ["code"=>1,"msg"=>'流水号不能为空'];
        }

        $data['body']=$order['body'];
        $data['total_fee']=$order['total_fee']*100;
        $data['product_id']=$order['product_id'];

        $data['attach']=$order['attach'];
        $data['out_trade_no']=$order['out_trade_no'];
        $data['notify_url']=$order['notify_url'];
        $data['is_sub_pay']=$order['is_sub_pay'];

        if($order['trade_type']=='JSAPI'){
            if($order['pay_type'] == AbstractPayment::PAY_SUB_CASH){
                $data['subOpenid']=empty($order['subOpenid'])?$order['openId']:$order['subOpenid'];
            }else{
                $data['openId']=$order['openId'];
            }
            $payment = $this->paymentService->jsapiPay($data);
            $payment['sequenceNo']=$data['out_trade_no'];
            return $payment;
        }else{
            $payment = $this->paymentService->nativePay($data);
            $payment['sequenceNo']=$data['out_trade_no'];
            return $payment;
        }
    }

    /**
     * 企业付款到零钱
     *
     * @param array $order
     * $order['body']               支付描述
     * $order['total_fee']          总金额
     * $order['openId']             用户openid 交易类型为JSAPI不能为空
     * $order['out_trade_no']       商户订单号(从账单接口获取)
     * $order['check_name']       校验用户姓名选项 NO_CHECK：不校验真实姓名 FORCE_CHECK：强校验真实姓名
     * $order['re_user_name']       收款用户姓名 如果check_name设置为FORCE_CHECK，则必填用户真实姓名
     * $order['handle']  = '0';      操作对象 1后台 0会员端
     *
     * @return array
     */
    public function changePay($order)
    {
        if(empty($order['body'])){
            return ["code"=>1,"msg"=>'支付描述不能为空'];
        }
        if(empty($order['total_fee'])){
            return ["code"=>1,"msg"=>'总金额不能为空'];
        }
        if(empty($order['openId'])){
            return ["code"=>1,"msg"=>'openId不能为空'];
        }
        if(empty($order['out_trade_no'])){
            return ["code"=>1,"msg"=>'流水号不能为空'];
        }
        if(empty($order['check_name'])){
            $order['check_name'] = 'FORCE_CHECK';
        }

        if($order['check_name'] == 'FORCE_CHECK' && empty($order['re_user_name'])){
            return ["code"=>1,"msg"=>'收款用户姓名不能为空'];
        }

        $data['total_fee'] = $order['total_fee']*100;
        $data['check_name'] = $order['check_name'];
        $data['re_user_name'] = !empty($order['re_user_name'])?$order['re_user_name']:'';
        $data['desc'] = $order['body'];
        $data['partner_trade_no'] = $order['out_trade_no'];
        $data['openid'] = $order['openId'];
        $data['handle'] = empty($order['handle'])?0:$order['handle'];
        $payment = $this->paymentService->changePay($data);
        $payment['sequenceNo']=$order['out_trade_no'];
        return $payment;
    }

    /**
     * 微信退款
     * @param array $order
     * $order['app_id']              应用id
     * $order["transaction_id"]     //微信回调返回的交易号
     * $order["targetId"]           //退款账单主键id
     * $order["detail"]           //详情
     * $order['is_sub_pay']         是否子商户支付 1 是 空为否
     * @return array
     * @throws \Exception
     */
    public function refund($order)
    {
        if(empty($order['app_id'])){
            return ["code"=>1,"msg"=>'应用id不能为空'];
        }
        if(empty($order['targetId'])){
            return ["code"=>1,"msg"=>'退款账单主键id不能为空'];
        }
        if(empty($order['transaction_id'])){
            return ["code"=>1,"msg"=>'交易号不能为空'];
        }

        # 发起微信退款接口
        $data['transaction_id'] =   $order['transaction_id'];
        $data['total_fee']  =   $order['orderAmount'];
        $data['refund_fee'] =    $order['refundAmount'];
        $data['refund_no']  = $order['refund_no'];
        $data['notify_url'] =    $order['notify_url'];
        $data['is_sub_pay']=$order['is_sub_pay'];
        $refund_result = $this->paymentService->refundOrder($data);
        if($refund_result['code']==0){
            return ['code'=>0,'msg'=>'退款成功'];
        }else{
            throw new \Exception($refund_result['msg']);
        }
    }
}
